package edu.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 获取请求头的内容
 * String getServerName(): 获取请求的服务器的名称，一般是个IP地址或域名
 * int getPort(): 返回当前请求的服务器的端口号
 * String getRequestURL() ：获取本次请求对应的URL
 * String getMethod()：获取本次请求的请求方式 ，可能是get 或 post方式
 * String getRequestURI：获取本次请求对应的URI
 * String getProtocol()：获取本次请求对应的协议和版本号
 * String getQueryString()：获取本次请求中的查询字符串(一般是链接中?之后的那部分)
 */

@WebServlet("/getRequestContent")
public class GetRequestContent extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String serverName = request.getServerName();
        int localPort = request.getLocalPort();
        int remotePort = request.getRemotePort();
        int serverPort = request.getServerPort();
        String requestURI = request.getRequestURI();
        StringBuffer URL = request.getRequestURL();
        String requestURL = URL.toString();
        String method = request.getMethod();
        String protocol = request.getProtocol();
        String queryString = request.getQueryString();

        System.out.println("服务器的名称"+serverName);//服务器的名称localhost
        System.out.println(localPort);  //80
        System.out.println(remotePort); //10759
        System.out.println(serverPort); //80
        System.out.println(requestURI); // /getRequestContent
        System.out.println(requestURL); // http://localhost/getRequestContent
        System.out.println(method);     //POST
        System.out.println(protocol);   //HTTP/1.1
        System.out.println(queryString);//null
    }
}
